﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using CNdotnet.Ezsocio.ContactService;

namespace CNdotnet.Ezsocio.ContactFromEmail
{
    internal static class WangyiResover
    {
        internal static List<ContactMember> Resove(string html)
        {
            List<ContactMember> contracts = new List<ContactMember>();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(html);
            var root = doc.DocumentElement;
            string code = root.SelectSingleNode("code").InnerText;
            if (code == "S_OK")
            {
                var objectNode = root.SelectSingleNode("array/object/array");
                var objectNodes = objectNode.SelectNodes("object");
                foreach (XmlNode n in objectNodes)
                {
                    ContactMember member = new ContactMember();
                    XmlNode n1 = n.SelectSingleNode("string[@name='EMAIL;PREF']");
                    if (n1 != null)
                    {
                        member.Email = n1.InnerText;
                        member.Name = member.Email;
                        XmlNode n2 = n.SelectSingleNode("string[@name='FN']");
                        if (n2 != null)
                        {
                            string name = n2.InnerText;
                            if (!string.IsNullOrEmpty(name))
                            {
                                member.Name = name;
                                if (name.StartsWith("/mg"))
                                {
                                    member.Name = name.Substring(3);
                                }
                            }
                        }
                        contracts.Add(member);
                    }
                }
            }
            return contracts;
        }
    }
}
